import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart' as ip;
import 'package:image_pickers/image_pickers.dart';

/// todo: 底部划出工具
class BottomSheet{
  static final  ip.ImagePicker _picker = ip.ImagePicker();

  /// todo：选择相册、拍照
  static bottomImagePick({
    Function(List<ImagePickModel>)? callBack,
    int selectCount:1
  }){
    Get.bottomSheet(
        Material(
          color: Colors.transparent,
          child: Container(
            height: 210,
            color: Colors.white,
            padding: EdgeInsets.symmetric(vertical: 15),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _cameraButton(callBack),
                Divider(color: Colors.grey.shade300,),
                _photosButton(callBack,selectCount: selectCount),
                Divider(color: Colors.grey.shade300,),
                _cancel(),
              ],
            ),
          ),
        )
    );
  }

  /// todo：相机按钮
  static _cameraButton(Function(List<ImagePickModel>)? call){
    return MaterialButton(onPressed: () async{
      List<ImagePickModel> _imgs = [];
      final ip.XFile? photo = await _picker.pickImage(source: ip.ImageSource.camera,imageQuality: 45);
      if(photo!=null){
        var date = await photo.lastModified();
        _imgs.add(ImagePickModel(name: photo.name,path: photo.path,lastModified: date));
      }
      call!(_imgs);
    },minWidth: Get.width * 0.7,child: Text("拍 照",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }

  /// todo：相册按钮
  static _photosButton(Function(List<ImagePickModel>)? call,{int selectCount:1}){
    return MaterialButton(onPressed: () async{
      List<ImagePickModel> _imgs = [];
      List<Media> _listImagePaths = await ImagePickers.pickerPaths(
        galleryMode: GalleryMode.image,
        selectCount: selectCount,
        showGif: false,
        showCamera: false,
        compressSize: 500,
        uiConfig: UIConfig(uiThemeColor: Color(0xff307b07)),
        cropConfig: CropConfig(enableCrop: false, width: 2, height: 1),
      );
      _listImagePaths.forEach((element) {
        _imgs.add(ImagePickModel(name: element.path!.split("/").last,path: element.path,lastModified: File(element.path??'').lastModifiedSync()));
      });
      call!(_imgs);
    },minWidth: Get.width * 0.7,child: Text("相 册",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }

  /// todo：取消按钮
  static _cancel(){
    return MaterialButton(onPressed: () async{
      Get.back();
    },child: Text("取 消",style: TextStyle(color: Colors.green,fontSize: 16,fontWeight: FontWeight.bold),),);
  }
}

class ImagePickModel{
  String? name;
  String? path;
  DateTime? lastModified;

  ImagePickModel({this.name, this.path, this.lastModified});
}